
(function($)
{$.modal=function(options)
{var settings=$.extend({},$.modal.defaults,options),root=getModalDiv(),winX=0,winY=0,contentWidth=0,contentHeight=0,mouseX=0,mouseY=0,resized,content='',contentObj;var titleClass=settings.title?'':' no-title';var title=settings.title?'<h1>'+settings.title+'</h1>':'';var borderOpen=settings.border?'"><div class="block-content'+titleClass:titleClass;var borderClose=settings.border?'></div':'';if(settings.useIframe)
{var sizeParts=new Array();if(settings.width)
{sizeParts.push('width="'+settings.width+'"');}
else if(settings.maxWidth)
{sizeParts.push('width="'+settings.maxWidth+'"');}
else
{sizeParts.push('width="'+settings.minWidth+'"');}
if(settings.height)
{sizeParts.push('height="'+settings.height+'"');}
else if(settings.maxHeight)
{sizeParts.push('height="'+settings.maxHeight+'"');}
else
{sizeParts.push('height="'+settings.minHeight+'"');}
var contentWrapper='<div class="modal-iframe-wrapper"><iframe src="'+settings.url+'" class="modal-content" frameborder="0" '+sizeParts.join(' ')+'></iframe></div>';}
else
{if(settings.content)
{if(typeof(settings.content)=='string')
{content=settings.content;}
else
{contentObj=settings.content.clone(true).show();}}
else
{content='';}
var sizeParts=new Array();sizeParts.push('min-width:'+settings.minWidth+'px;');sizeParts.push('min-height:'+settings.minHeight+'px;');if(settings.width)
{sizeParts.push('width:'+settings.width+'px; ');}
if(settings.height)
{sizeParts.push('height:'+settings.height+'px; ');}
if(settings.maxWidth)
{sizeParts.push('max-width:'+settings.maxWidth+'px; ');}
if(settings.maxHeight)
{sizeParts.push('max-height:'+settings.maxHeight+'px; ');}
var contentStyle=(sizeParts.length>0)?' style="'+sizeParts.join(' ')+'"':'';var scrollClass=settings.scrolling?' modal-scroll':'';var contentWrapper='<div class="modal-content'+scrollClass+'"'+contentStyle+'>'+content+'</div>';}
var win=$('<div class="modal-window block-border'+borderOpen+'">'+title+contentWrapper+'</div'+borderClose+'>').appendTo(root);var contentBlock=win.find('.modal-content');var contentBlockWrapper=settings.useIframe?contentBlock.parent():contentBlock;if(contentObj)
{contentObj.appendTo(contentBlockWrapper);}
if(settings.resizable&&settings.border)
{var resizeFunc=function(event)
{var offsetX=event.pageX-mouseX,offsetY=event.pageY-mouseY,newWidth=Math.max(settings.minWidth,contentWidth+(resized.width*offsetX)),newHeight=Math.max(settings.minHeight,contentHeight+(resized.height*offsetY)),correctX=0,correctY=0;if(settings.maxWidth&&newWidth>settings.maxWidth)
{correctX=newWidth-settings.maxWidth;newWidth=settings.maxWidth;}
if(settings.maxHeight&&newHeight>settings.maxHeight)
{correctY=newHeight-settings.maxHeight;newHeight=settings.maxHeight;}
if(settings.useIframe)
{contentBlock.attr('width',newWidth).attr('height',newHeight);}
else
{contentBlock.css({width:newWidth+'px',height:newHeight+'px'});}
win.css({left:(winX+(resized.left*(offsetX+correctX)))+'px',top:(winY+(resized.top*(offsetY+correctY)))+'px'});};$('<div class="modal-resize-tl"></div>').appendTo(win).data('modal-resize',{top:1,left:1,height:-1,width:-1}).add($('<div class="modal-resize-t"></div>').appendTo(win).data('modal-resize',{top:1,left:0,height:-1,width:0})).add($('<div class="modal-resize-tr"></div>').appendTo(win).data('modal-resize',{top:1,left:0,height:-1,width:1})).add($('<div class="modal-resize-r"></div>').appendTo(win).data('modal-resize',{top:0,left:0,height:0,width:1})).add($('<div class="modal-resize-br"></div>').appendTo(win).data('modal-resize',{top:0,left:0,height:1,width:1})).add($('<div class="modal-resize-b"></div>').appendTo(win).data('modal-resize',{top:0,left:0,height:1,width:0})).add($('<div class="modal-resize-bl"></div>').appendTo(win).data('modal-resize',{top:0,left:1,height:1,width:-1})).add($('<div class="modal-resize-l"></div>').appendTo(win).data('modal-resize',{top:0,left:1,height:0,width:-1})).mousedown(function(event)
{contentWidth=contentBlock.width();contentHeight=contentBlock.height();var position=win.position();winX=position.left;winY=position.top;mouseX=event.pageX;mouseY=event.pageY;resized=$(this).data('modal-resize');event.preventDefault();$(document).bind('mousemove',resizeFunc);})
root.mouseup(function()
{$(document).unbind('mousemove',resizeFunc);});}
win.mousedown(function()
{$(this).putModalOnFront();});if(settings.draggable&&title)
{var moveFunc=function(event)
{var width=win.outerWidth(),height=win.outerHeight();win.css({left:Math.max(0,Math.min(winX+(event.pageX-mouseX),$(root).width()-width))+'px',top:Math.max(0,Math.min(winY+(event.pageY-mouseY),$(root).height()-height))+'px'});};win.find('h1:first').mousedown(function(event)
{var position=win.position();winX=position.left;winY=position.top;mouseX=event.pageX;mouseY=event.pageY;event.preventDefault();$(document).bind('mousemove',moveFunc);});root.mouseup(function()
{$(document).unbind('mousemove',moveFunc);});}
if(settings.closeButton)
{$('<ul class="action-tabs right"><li><a href="#" title="Close window"><img src="' + settings.imgClose + '" width="16" height="16"></a></li></ul>').prependTo(win).find('a').click(function(event)
{event.preventDefault();$(this).closest('.modal-window').closeModal();});}
var buttonsFooter=false;$.each(settings.buttons,function(key,value)
{if(!buttonsFooter)
{buttonsFooter=$('<div class="block-footer align-'+settings.buttonsAlign+'"></div>').insertAfter(contentBlockWrapper);}
else
{buttonsFooter.append('&nbsp;');}
$('<button type="button">'+key+'</button>').appendTo(buttonsFooter).click(function(event)
{value.call(this,$(this).closest('.modal-window'),event);});});if(settings.onClose)
{win.bind('closeModal',settings.onClose);}
win.applyTemplateSetup();if(!root.is(':visible'))
{win.hide();root.fadeIn('normal',function()
{win.show().centerModal();});}
else
{win.centerModal();}
$.modal.current=win;$.modal.all=root.children('.modal-window');if(settings.onOpen)
{settings.onOpen.call(win.get(0));}
if(settings.url)
{win.loadModalContent(settings.url,settings);}
return win;};$.modal.current=false;$.modal.all=$();$.fn.modal=function(options)
{var modals=$();this.each(function()
{modals.add($.modal($.extend(options,{content:$(this).clone(true).show()})));});return modals;};$.fn.getModalContentBlock=function()
{return this.find('.modal-content');}
$.fn.getModalWindow=function()
{return this.closest('.modal-window');}
$.fn.setModalContent=function(content,resize)
{this.each(function()
{var contentBlock=$(this).getModalContentBlock().not('iframe');if(contentBlock.length>0)
{if(typeof(content)=='string')
{contentBlock.html(content);}
else
{content.clone(true).show().appendTo(contentBlock);}
contentBlock.applyTemplateSetup();if(resize)
{contentBlock.setModalContentSize(true,false);}}});return this;}
$.fn.setModalContentSize=function(width,height)
{this.each(function()
{var contentBlock=$(this).getModalContentBlock(),useIframe=contentBlock.is('iframe');if(width!==true)
{if(useIframe)
{if(width)
{contentBlock.attr('width',width);}}
else
{contentBlock.css('width',width?width+'px':'');}}
if(height!==true)
{if(useIframe)
{if(height)
{contentBlock.attr('height',height);}}
else
{contentBlock.css('height',height?height+'px':'');}}});return this;}
$.fn.loadModalContent=function(url,options)
{var settings=$.extend({loadingMessage:'',data:{},complete:function(responseText,textStatus,XMLHttpRequest){},resize:true,resizeOnMessage:false,resizeOnLoad:false},options)
this.each(function()
{var win=$(this),contentBlock=win.getModalContentBlock(),useIframe=contentBlock.is('iframe');if(useIframe)
{contentBlock.attr('src',url);}
else
{if(settings.loadingMessage)
{win.setModalContent('<div class="modal-loading">'+settings.loadingMessage+'</div>',(settings.resize||settings.resizeOnMessage));}
contentBlock.load(url,settings.data,function(responseText,textStatus,XMLHttpRequest)
{var hidden=false;if(win.is(':hidden'))
{win.show();hidden=true;}
contentBlock.applyTemplateSetup();if(settings.resize||settings.resizeOnLoad)
{contentBlock.setModalContentSize(true,false);}
settings.complete.call(this,responseText,textStatus,XMLHttpRequest);if(hidden)
{win.hide();}});}});return this;}
$.fn.setModalTitle=function(newTitle)
{this.each(function()
{var win=$(this),title=$(this).find('h1'),contentBlock=win.hasClass('block-content')?win:win.children('.block-content:first');if(newTitle.length>0)
{if(title.length==0)
{contentBlock.removeClass('no-title');title=$('<h1>'+newTitle+'</h1>').prependTo(contentBlock);}
title.html(newTitle);}
else if(title.length>0)
{title.remove();contentBlock.addClass('no-title');}});return this;}
$.fn.addButtons=function(buttons,clear)
{var win=$.modal.current,buttonsFooter=win.find('.block-footer');contentDiv=win.find('.modal-content');if(clear)
{buttonsFooter.children().remove();}
$.each(buttons,function(key,value)
{buttonsFooter.append('&nbsp;');$('<button type="button">'+key+'</button>').appendTo(buttonsFooter).click(function(event)
{value.call(this,$(this).getModalWindow(),event);});});};$.fn.centerModal=function(animate)
{var root=getModalDiv(),rootW=root.width()/2,rootH=root.height()/2;this.each(function()
{var win=$(this),winW=Math.round(win.outerWidth()/2),winH=Math.round(win.outerHeight()/2);win[animate?'animate':'css']({left:(rootW-winW)+'px',top:(rootH-winH)+'px'});});return this;};$.fn.putModalOnFront=function()
{if($.modal.all.length>1)
{var root=getModalDiv();this.each(function()
{if($(this).next('.modal-window').length>0)
{$(this).detach().appendTo(root);}});}
return this;};$.fn.closeModal=function()
{this.each(function()
{var event=$.Event('closeModal'),win=$(this);win.trigger(event);if(!event.isDefaultPrevented())
{win.remove();var root=getModalDiv();$.modal.all=root.children('.modal-window');if($.modal.all.length==0)
{$.modal.current=false;root.fadeOut('normal');}
else
{$.modal.current=$.modal.all.last();}}});return this;};$.modal.defaults={content:false,useIframe:false,url:false,title:false,border:true,draggable:true,resizable:true,scrolling:true,closeButton:true,buttons:{},buttonsAlign:'right',onOpen:false,onClose:false,minHeight:40,minWidth:200,maxHeight:false,maxWidth:false,height:false,width:450,loadingMessage:'Loading...',data:{},complete:function(responseText,textStatus,XMLHttpRequest){},resize:true,resizeOnMessage:false,resizeOnLoad:false};function getModalDiv()
{var modal=$('#modal');if(modal.length==0)
{var target=$(document.body),ieDiv=target.children('.ie, ie7');if(ieDiv.length>0)
{ieDiv.eq(0).append('<div id="modal"></div>');}
else
{target.append('<div id="modal"></div>');}
modal=$('#modal').hide();}
return modal;};})(jQuery);